/* -----------------
* brief
* 1. this is code gen by tools
 */

package service

import (
	"fmt"
	"github.com/mjiulee/lego"
	"saas_sys_base/common"
)
import . "saas_sys_base/modules/adminSaas/models"

type AdminSaasMenuService struct {
	// add your model here
	tbAdminSaasMenu AdminSaasMenu
}


func (t *AdminSaasMenuService) ListAdminSaasMenu(miniAppId int64, menuMode int,pid int64, keyword string, page, psize int) ([]map[string]string, int64, error) {
	// 业务查询
	queryMap := map[string]interface{}{
		"table":  map[string]string{"table": "tb_admin_saas_menu", "alias": "t1"},
		"fields": []string{"t1.*"},
		"join":   []map[string]string{
			// map[string]string{"table": "tb_xx as t2", "on": "t2.xid = t1.id"},
		},
		"where": []lego.QMapWhereField{
			{"t1.if_del", "=", 0, "AND", false},
			{"t1.pid", "=", pid, "AND", false},
			{"t1.mini_app_id", "=", miniAppId, "AND", false},
			{"t1.menu_mode", "=", menuMode, "AND", false},
		},
		"search": map[string]string{
			//"t1.id": req.Keyword,
		},
		"orders": []string{"t1.id asc"},
		"page": map[string]int{
			"page":  page,
			"psize": psize,
		},
	}
	return lego.QueryListByMap(queryMap)
}

func (t *AdminSaasMenuService) ListTopMenu(who *common.Who) []AdminSaasMenu {
	return t.tbAdminSaasMenu.ListTopMenu(who)
}

/* 根据id获取记录
 * -----------------
 */
func (t *AdminSaasMenuService) GetAdminSaasMenuById(id int64) *AdminSaasMenu {
	return t.tbAdminSaasMenu.RecodeById(id)
}


/* 根据菜单名称获取记录
 * -----------------
 */
func (t *AdminSaasMenuService) GetAdminSaasMenuByName(name string) *AdminSaasMenu {
	return t.tbAdminSaasMenu.RecodeByName(name)
}

/* 添加
 * -----------------
 */
func (t *AdminSaasMenuService) SaveAdminSaasMenu(item2add *AdminSaasMenu) bool {
	return t.tbAdminSaasMenu.AddRecode(item2add)
}

/* 删除(硬删除)*/
func (t *AdminSaasMenuService) DelAdminSaasMenuById(id int64) bool {
	return t.tbAdminSaasMenu.DelRecodeById(id)
}

/* 更新
 * -----------------*/
func (t *AdminSaasMenuService) UpdateAdminSaasMenu(rc *AdminSaasMenu) bool {
	return t.tbAdminSaasMenu.UpdateRecode(rc)
}

/*菜单项的名称是否已经存在，菜单项存在的标准是，菜单名称相等，且两者的父菜单名称也相等*/
/*
menuConf *SysMenu, 菜单配置项
parentName *string， 其父菜单名字
rcMenuDb *SysMenu：为空表示不存在
*/
func (t *AdminSaasMenuService) GetMenuByName(appwho *common.Who, menuConf *AdminSaasMenu, parentName *string) (rcMenuDb *AdminSaasMenu, isExist bool) {
	menuListDb := t.tbAdminSaasMenu.ListByNameAndMenuMode(appwho, menuConf.Name, menuConf.MenuMode) //加入筛选MenuMode，区分app管理端和租户管理端
	for _,rcMenuDb := range menuListDb {
		/*rcMenuDb = t.tbSysMenu.RecodeByName(menuConf.Name)
		if rcMenuDb == nil {
			return nil, false //不存在，为不相等
		}*/

		if rcMenuDb.Name != menuConf.Name {
			continue //return nil, false //不相等
		}

		//菜单名与数据库的一致，继续比较父菜单
		if menuConf.Pid == 0 &&  rcMenuDb.Pid == 0 {
			return &rcMenuDb, true //同为一级菜单，名字也一样，则表示已存在,相等
		}

		//两者同时为2级菜单，比较父菜单名字
		if menuConf.Pid != 0 &&  rcMenuDb.Pid != 0 {
			rcParentMenuDb := rcMenuDb.RecodeByIdAndMenuMode(rcMenuDb.Pid, menuConf.MenuMode) //加入筛选MenuMode，区分app管理端和租户管理端
			if rcParentMenuDb == nil { //这里查出来时nil，是因为父菜单被删除了，目前的逻辑删除父菜单不会关联删除子菜单
				continue //未找到父菜单，算是匹配不成功，认为是两菜单不一样
			}
			if rcParentMenuDb.Name == *parentName {
				return &rcMenuDb, true
			} else {
				continue // break //return nil, false
			}
		} else { //级数不同，不相等
			continue //nil, false
		}
	}

	return nil, false //对比完所有都没有找到，则不相等，即不存在
}

func (t *AdminSaasMenuService) CheckMenuConfig(menulist []AdminSaasMenu) {
	for _, menuConf := range menulist {
		if menuConf.MType != 1 && menuConf.MType != 2 {
			panic(fmt.Sprintf("menuConf.MType must be 1 or 2, but now it is <%d>", menuConf.MType))
		}

		if (menuConf.MType == 2 && menuConf.Pid != -1) ||
			(menuConf.MType != 2 && menuConf.Pid == -1) {
			panic("if menuConf.MType  2(submenu), pid must be -1")
		}

		if (menuConf.MenuMode != common.MenuMode_MiniApp && menuConf.MenuMode != common.MenuMode_Tenant) {
			panic("menuConf.MenuMode invalid")
		}
	}

}